// HorizontalScrollbar.cpp : implementation file
//

#include "stdafx.h"
#include "BBSimple.h"
#include "HorizontalScrollbar.h"
#include "MEMDC.H"


// CHorizontalScrollbar

IMPLEMENT_DYNAMIC(CHorizontalScrollbar, CStatic)

CHorizontalScrollbar::CHorizontalScrollbar()
{
	m_bMouseDown			= FALSE;
	m_bMouseDownArrowLeft	= FALSE;
	m_bMouseDownArrowRight	= FALSE;
	m_bDragging				= FALSE;

	m_nThumbLeft			= 25;
	m_dbThumbRemainder		= 0.00f;
	m_dbThumbInterval		= 0.000000;

	m_pList					= NULL;
}

CHorizontalScrollbar::~CHorizontalScrollbar()
{
}


BEGIN_MESSAGE_MAP(CHorizontalScrollbar, CStatic)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_WM_CREATE()
END_MESSAGE_MAP()


int CHorizontalScrollbar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CStatic::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

// CHorizontalScrollbar message handlers
void CHorizontalScrollbar::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	Draw();
}

void CHorizontalScrollbar::OnLButtonDown(UINT nFlags, CPoint point) 
{
	SetCapture();
	CRect clientRect;
	GetClientRect(&clientRect);

	int nWidth = clientRect.Width()-26;

	CRect rectLeftArrow(0, 0, 26, 20);
	CRect rectRightArrow(nWidth, 0, nWidth+26, 20);
	CRect rectThumb(m_nThumbLeft, 0, m_nThumbLeft+26, 20);

	if (rectThumb.PtInRect(point))
	{
		m_bMouseDown = TRUE;
	}


	if (rectRightArrow.PtInRect(point))
	{
		m_bMouseDownArrowRight = TRUE;
		SetTimer(2,250,NULL);
	}

	if (rectLeftArrow.PtInRect(point))
	{
		m_bMouseDownArrowLeft = TRUE;
		SetTimer(2,250,NULL);
	}

	CStatic::OnLButtonDown(nFlags, point);
}

void CHorizontalScrollbar::OnLButtonUp(UINT nFlags, CPoint point) 
{
	UpdateThumbPosition();
	KillTimer(1);
	ReleaseCapture();


	BOOL bInChannel = TRUE;

	CRect clientRect;
	GetClientRect(&clientRect);

	int nWidth = clientRect.Width()-26;

	CRect rectLeftArrow(0, 0, 26, 20);
	CRect rectThumb(m_nThumbLeft, 0, m_nThumbLeft+26, 20);

	if (rectLeftArrow.PtInRect(point))
	{
		ScrollLeft();	
		bInChannel = FALSE;
	}

	CRect rectRightArrow(nWidth,0,nWidth+26,20);


	if (rectRightArrow.PtInRect(point))
	{
		ScrollRight();	
		bInChannel = FALSE;
	}

	if (rectThumb.PtInRect(point))
	{
		bInChannel = FALSE;
	}

	if (bInChannel && !m_bMouseDown)
	{
		if (point.x > m_nThumbLeft)
		{
			PageRight();
		}
		else
		{
			PageLeft();
		}
	}

	//reset all variables
	m_bMouseDown		   = FALSE;
	m_bDragging			   = FALSE;
	m_bMouseDownArrowLeft  = FALSE;
	m_bMouseDownArrowRight = FALSE;
	CStatic::OnLButtonUp(nFlags, point);
}

void CHorizontalScrollbar::OnMouseMove(UINT nFlags, CPoint point) 
{
	CRect clientRect;
	GetClientRect(&clientRect);

	if (m_bMouseDown)
		m_bDragging = TRUE;

	if (m_bDragging)
	{	
		m_nThumbLeft = point.x-13; //-13 so mouse is in middle of thumb

		double nMax = m_pList->GetScrollLimit(SB_HORZ);
		int nPos = m_pList->GetScrollPos(SB_HORZ);

		double nWidth = clientRect.Width()-75;
		double nVar = nMax;
		m_dbThumbInterval = nWidth/nVar;

		//figure out how many times to scroll total from top
		//then minus the current position from it
		int nScrollTimes = (int)((m_nThumbLeft-25)/m_dbThumbInterval)-nPos;

		CSize size;
		size.cx = nScrollTimes;
		size.cy = 0;

		m_pList->Scroll(size);

		LimitThumbPosition();

		Draw();
	}

	CStatic::OnMouseMove(nFlags, point);
}

void CHorizontalScrollbar::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == 1)
	{
		if (m_bMouseDownArrowRight)
		{
			ScrollRight();
		}

		if (m_bMouseDownArrowLeft)
		{
			ScrollLeft();
		}
	}
	else if (nIDEvent == 2)
	{
		if (m_bMouseDownArrowRight)
		{
			KillTimer(2);
			SetTimer(1, 50, NULL);
		}

		if (m_bMouseDownArrowLeft)
		{
			KillTimer(2);
			SetTimer(1, 50, NULL);
		}
	}

	CStatic::OnTimer(nIDEvent);
}

void CHorizontalScrollbar::ScrollLeft()
{
	m_pList->SendMessage(WM_HSCROLL, MAKELONG(SB_LINELEFT,0),NULL);
	UpdateThumbPosition();
}

void CHorizontalScrollbar::ScrollRight()
{
	m_pList->SendMessage(WM_HSCROLL, MAKELONG(SB_LINERIGHT,0),NULL);
	UpdateThumbPosition();
}

void CHorizontalScrollbar::UpdateThumbPosition()
{
	CRect clientRect;
	GetClientRect(&clientRect);

	double nPos = m_pList->GetScrollPos(SB_HORZ);
	double nMax = m_pList->GetScrollLimit(SB_HORZ);
	double nWidth = clientRect.Width()-75; 
	double nVar = nMax;

	m_dbThumbInterval  = nWidth/nVar;

	double nNewdbValue = m_dbThumbInterval * (nPos);
	int nNewValue	   = (int)nNewdbValue;
	double nExtra	   = nNewdbValue - nNewValue;
	m_dbThumbRemainder = nExtra;

	m_nThumbLeft = 25 + nNewValue;

	LimitThumbPosition();

	Draw();
}

void CHorizontalScrollbar::PageRight()
{
	m_pList->SendMessage(WM_HSCROLL, MAKELONG(SB_PAGEDOWN,0),NULL);
	UpdateThumbPosition();
}

void CHorizontalScrollbar::PageLeft()
{
	m_pList->SendMessage(WM_HSCROLL, MAKELONG(SB_PAGEUP,0),NULL);
	UpdateThumbPosition();
}

void CHorizontalScrollbar::Draw()
{
	CClientDC dc(this);
	CRect clientRect;
	GetClientRect(&clientRect);
	CMemoryDC memDC(&dc, &clientRect);
	memDC.FillSolidRect(&clientRect,  RGB(76,85,118));

	CDC bitmapDC;
	bitmapDC.CreateCompatibleDC(&dc);

	//draw left arrow of scrollbar
	CBitmap *pBmp, *pOldBitmap;
	HBITMAP hbmpImage_left;
	HBITMAP hbmpImage_span;
	HBITMAP hbmpImage_right;
	HBITMAP hbmpImage_thumb;
	
	CString strImage;
	strImage.Format(_T("%s\\Scrollbar\\HorizontalScrollBarLeftArrow.bmp"), theApp.m_strImageRoot);

	hbmpImage_left = (HBITMAP)LoadImage(GetModuleHandle(NULL), strImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	pBmp = CBitmap::FromHandle(hbmpImage_left);
	pOldBitmap = bitmapDC.SelectObject(pBmp);

	memDC.BitBlt(clientRect.left, clientRect.top, 26, 12, &bitmapDC, 0, 0, SRCCOPY);

	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;

	strImage.Format(_T("%s\\Scrollbar\\HorizontalScrollBarSpan.bmp"), theApp.m_strImageRoot);
	hbmpImage_span = (HBITMAP)LoadImage(GetModuleHandle(NULL), strImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	pBmp = CBitmap::FromHandle(hbmpImage_span);
	pOldBitmap = bitmapDC.SelectObject(pBmp);

	int nWidth = clientRect.Width() - 26;

	memDC.StretchBlt(clientRect.left+26, clientRect.top, nWidth, 12, &bitmapDC, 0, 0, 1, 12, SRCCOPY);

	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;

	strImage.Format(_T("%s\\Scrollbar\\HorizontalScrollBarRightArrow.bmp"), theApp.m_strImageRoot);
	hbmpImage_right = (HBITMAP)LoadImage(GetModuleHandle(NULL), strImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
	pBmp = CBitmap::FromHandle(hbmpImage_right);
	pOldBitmap = bitmapDC.SelectObject(pBmp);

	memDC.BitBlt(nWidth,clientRect.top,26,12,&bitmapDC,0,0,SRCCOPY);

	bitmapDC.SelectObject(pOldBitmap);
	pOldBitmap = NULL;

	//If there is nothing to scroll then don't
	//show the thumb control otherwise show it
	if (m_pList->GetScrollLimit(SB_HORZ) != 0)
	{
		strImage.Format(_T("%s\\Scrollbar\\HorizontalScrollBarThumb.bmp"), theApp.m_strImageRoot);
		hbmpImage_thumb = (HBITMAP)LoadImage(GetModuleHandle(NULL), strImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
		pBmp = CBitmap::FromHandle(hbmpImage_thumb);
		pOldBitmap = bitmapDC.SelectObject(pBmp);
		memDC.BitBlt(clientRect.left+m_nThumbLeft, clientRect.top, 26, 12, &bitmapDC, 0, 0, SRCCOPY);

		bitmapDC.SelectObject(pOldBitmap);
		pOldBitmap = NULL;
		DeleteObject(hbmpImage_thumb);
	}

 	DeleteObject(hbmpImage_left);
 	DeleteObject(hbmpImage_right);
 	DeleteObject(hbmpImage_span);
	
}

void CHorizontalScrollbar::LimitThumbPosition()
{
	CRect clientRect;
	GetClientRect(&clientRect);

	if (m_nThumbLeft+26 > (clientRect.Width()-26))
	{
		m_nThumbLeft = clientRect.Width()-51;
	}

	if (m_nThumbLeft < (clientRect.left+25))
	{
		m_nThumbLeft = clientRect.left+25;
	}
}



